import { Buffer } from 'buffer';

console.log('--------- 实例1 ---------')
//实例1-1-1
const bufA01 = Buffer.from('abc', 'utf8');
console.log('实例1-1-1', bufA01);//<Buffer 61 62 63>
console.log('实例1-1-1', bufA01.toString());//abc
//实例1-1-2
const bufA02 = Buffer.from('abc', 'hex');
console.log('实例1-1-2', bufA02);//<Buffer ab>
console.log('实例1-1-2', bufA02.toString('hex'));//ab

//实例1-2
const bufA03 = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
console.log('实例1-2', bufA03);//<Buffer 62 75 66 66 65 72>
console.log('实例1-2', bufA03.toString());//buffer

//实例1-3
const bufA04 = Buffer.from('buffer');
const bufA05 = Buffer.from(bufA04);
bufA05[0] = 0x61;//索引运算符 [index] 可用于获取和设置 buf 中位置 index 处的八位字节。 这些值是指单个字节，因此合法值范围介于 0x00 和 0xFF（十六进制）或 0 和 255（十进制）之间。
console.log('实例1-3', bufA04.toString());//buffer
console.log('实例1-3', bufA05.toString());//auffer

//实例1-4-1
const bufA06 = Buffer.from(new String('abc'));
console.log('实例1-4-1', bufA06);//<Buffer 61 62 63>
//实例1-4-2
class Foo {
	[Symbol.toPrimitive]() {
		return 'abc';
	}
}
const bufA07 = Buffer.from(new Foo(), 'utf8');
console.log('实例1-4-2', bufA07);//<Buffer 61 62 63>